const fs = require('fs')
const path = require('path')
const babylon = require('babylon');
const babel = require('babel-core')
const t = require('@babel/types')
const generate = require('@babel/generator').default
const traverse = require('@babel/traverse');
let types = require('babel-types');
// var TemplateParser = require('./TemplateParser')

const jsPlugin = require('./plugin')

const outDir = path.resolve(__dirname, './dist')
// 1. 读取源码
const orgCode = fs.readFileSync(path.resolve(__dirname, './src/App.vue'), 'utf-8')
// console.log(orgCode)
// 2. 将源码解析为template script style三个部分

// ======== 3. 解析转换html
let template = orgCode.match(/<template>([\s\S]*)<\/template>/)[1]
template = template.replace(/div/g, 'View')
const htmlPath = path.resolve(__dirname, './dist/app.wxml')
fs.writeFileSync(htmlPath, template, 'utf-8')


//  ======= 4. 解析转换js模块
const script = orgCode.match(/<script>([\s\S]*)<\/script>/)[1]
const AST = babel.transform(script, { sourceType: 'module', plugins: [jsPlugin] })
const jsOutPath = path.resolve(__dirname, './dist/app.js')
fs.writeFileSync(jsOutPath, AST.code, 'utf-8')


//  ======== 5. 解析转换css模块
const style = orgCode.match(/<style (.*)>([\s\S]*)<\/style>/)[2]
const cssOutPath = path.resolve(__dirname, './dist/app.less')
fs.writeFileSync(cssOutPath, style, 'utf-8')



